package com.ygqh.baby.handler;

import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.model.*;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.YgProductService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 〈壹家店首页商品〉
 *
 * @author guohao
 * @date 2019/4/23 12:03
 * @since 1.0.0
 */
@Component
public class HomePageProductHandler {

    private static final String HOME_PAGE_PRODUCT_KEY = "HOME_PAGE_PRODUCT_KEY";

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private YgProductService ygProductService;

    public HomePageProduct getHomePageProduct() {
        String res = redisDao.get(HOME_PAGE_PRODUCT_KEY);
        HomePageProduct homePageProduct = null;
        if (StringUtil.isNotBlank(res)) {
            homePageProduct = JSON.parseObject(res, HomePageProduct.class);
        }
        return homePageProduct;
    }

    public HomePageProduct getHomePageProductInfo() {
        String res = redisDao.get(HOME_PAGE_PRODUCT_KEY);
        HomePageProduct homePageProduct = null;
        if (StringUtil.isNotBlank(res)) {
            homePageProduct = JSON.parseObject(res, HomePageProduct.class);
            List<HomeProductData> products = homePageProduct.getpList();
            if (CollectionUtils.isEmpty(products)) {
                return homePageProduct;
            }
            Long[] pids = homePageProduct.getpIdList().toArray(new Long[]{});
            ProductSelectListParam selectListParam = new ProductSelectListParam();
            selectListParam.setpIds(pids);
            List<Product4SelectListModel> list4Select = ygProductService.findList4Select(selectListParam);
            products.sort(Comparator.comparingInt(HomeProductData::getSortNum));
            setProductInfo(products, list4Select);
        }
        return homePageProduct;
    }

    private void setProductInfo(List<HomeProductData> products, List<Product4SelectListModel> list4Select) {
        for (HomeProductData product : products) {
            for (Product4SelectListModel model : list4Select) {
                if (product.getProductId().equals(model.getProductId())) {
                    product.setProductInfo(model);
                }
            }
        }

    }

    public Message setHomePageProduct(HomePageProduct homePageProduct) {
        Assert.notNull(homePageProduct, "首页商品不能为空");
        List<Long> pids = homePageProduct.getpList().stream().map(HomeProductData::getProductId).collect(Collectors.toList());
        homePageProduct.setpIdList(pids);
        List<YgProduct> productList = ygProductService.findByIds(pids.toArray(new Long[0]));
        if (CollectionUtils.isNotEmpty(productList)) {
            List<Long> scIdList = productList.stream().map(YgProduct::getGroupId)
                    .collect(Collectors.toList());
            homePageProduct.setScIdList(scIdList);
        }
        String set = redisDao.set(HOME_PAGE_PRODUCT_KEY, JSON.toJSONString(homePageProduct), 0);
        return Message.success(set);
    }
}
